package com.jnshu.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jnshu.constant.Constant;
import com.jnshu.dao.RoleMapper;
import com.jnshu.dto.admin.RoleDto;
import com.jnshu.pojo.Role;
import com.jnshu.service.RoleService;
import com.jnshu.tools.HeadUtil;
import com.jnshu.tools.binding.JsonUtil;
import com.jnshu.tools.resultUtils.PageBean;
import com.jnshu.tools.resultUtils.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
public class RoleCon {

    @Resource
    RoleService roleService;

    @GetMapping(value = "/role/list")
    public PageBean getRoleList(@RequestParam(name = "pageSize",defaultValue = "10")int pageSize,
                                @RequestParam(name = "pageNum",defaultValue = "1")int pageNum){
        log.info ("_____________________________________________");
        log.info("方法getRoleList接收到的请求参数为："+pageNum+","+pageSize);
        return roleService.selectRoleList(pageNum,pageSize);
    }

    @DeleteMapping(value = "/role/{id}")
    public ResultBean deleteRole(@PathVariable("id")long id){
        log.info ("_____________________________________________");
        log.info("方法deleteRole接收到的请求参数为："+id);
        return roleService.deleteByPrimaryKey(id);
    }

    @PutMapping(value = "/role/",produces = {"application/json;charset=UTF-8"})
    public ResultBean updateRole(@RequestBody RoleDto roleDto){
        log.info ("_____________________________________________");
        log.info("方法updateRole接收到的请求参数为："+ JsonUtil.toJsonString(roleDto));
        return roleService.updateByPrimaryKeySelective(roleDto, HeadUtil.getHeadValue(Constant.LOGINID));
    }

    @PostMapping(value = "/role",produces = {"application/json;charset=UTF-8"})
    public ResultBean insertRole(@RequestBody RoleDto roleDto){
        log.info ("_____________________________________________");
        log.info("方法insertRole接收到的请求参数为："+JsonUtil.toJsonString(roleDto));
        return roleService.insertSelective(roleDto, HeadUtil.getHeadValue(Constant.LOGINID));
    }

    @GetMapping(value = "/user/{id}")
    public ResultBean selectRoleById(@PathVariable("id")long id){
        log.info ("_____________________________________________");
        log.info("方法selectRoleById接收到的请求参数为："+id);
        return roleService.selectRoleById(id);
    }

}
